package com.intellagric.service.traceability.Impl.trace_process;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.TraceProcessMapper;
import com.intellagric.pojo.TracePersonType;
import com.intellagric.pojo.TraceProcess;
import com.intellagric.pojo.TraceProcessExample;
import com.intellagric.service.traceability.trace_process.ProcessService;
import org.aspectj.weaver.tools.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2019/1/12
 * @DESC： 生产流程配置管理服务
 */
@Service
public class ProcessServiceImpl implements ProcessService {

    @Autowired
    private TraceProcessMapper processMapper;

    /**
     * 添加生产流程配置信息
     * @param process
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addProcess(TraceProcess process) {
        if (processMapper.insertSelective(process) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除生产流程配置信息
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteProcess(String id) {
        if (processMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 编辑生产流程配置信息
     * @param process
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editProcess(TraceProcess process) {
        if (processMapper.updateByPrimaryKeySelective(process) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 通过id查询生产流程配置信息
     * @param id
     * @return TracePersonType
     */
    @Override
    public TraceProcess getProcessById(String id) {
        return processMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据父id和批次id查询生产流程配置信息列表
     * @param parentId
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getProcessByParentId(String parentId,String produceBatchId) {
        TraceProcessExample processExample = new TraceProcessExample();
        processExample.setOrderByClause("create_date");
        processExample.createCriteria().andParentIdEqualTo(parentId).andProduceBatchIdEqualTo(produceBatchId);
        List<TraceProcess> processList = processMapper.selectByExample(processExample);
        return LayuiDataGridResult.success().add(processList,processList.size());
    }

    /**
     * 根据批次id查询生产流程配置信息列表
     * @param produceBatchId
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getProcessList(String produceBatchId) {
        TraceProcessExample processExample = new TraceProcessExample();
        processExample.setOrderByClause("create_date");
        processExample.createCriteria().andProduceBatchIdEqualTo(produceBatchId);
        List<TraceProcess> processList = processMapper.selectByExample(processExample);
        return LayuiDataGridResult.success().add(processList,processList.size());
    }

    /**
     * 根据批次id查询生产流程配置信息列表，并生成树形列表
     * @param produceBatchId
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getProcessTree(String produceBatchId) {
        List<TraceProcess> processList = this.getProcessList(produceBatchId).getData();

        List<TraceProcess> rootProcessList = this.getProcessByParentId("0", produceBatchId).getData();

        for (TraceProcess process : rootProcessList) {
            process.setChildren(getChild(process.getId(), processList));
        }
        return LayuiDataGridResult.success().add(rootProcessList,rootProcessList.size());
    }

    private List<TraceProcess> getChild(String id, List<TraceProcess> processList) {
        // 子菜单
        List<TraceProcess> childList = new ArrayList<>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (TraceProcess process : processList) {
            if (process.getParentId() != null) {   //所有非以及菜单的都要加入chidList中（递归）
                if (process.getParentId().equals(id)) {
                    childList.add(process);
                }
            }
        }

        // 把子菜单的子菜单再循环一遍
        for (TraceProcess process : childList) {// 没有url子菜单还有子菜单（暂不考虑url的问题）
            // 递归
            process.setChildren(getChild(process.getId(), processList));
        } // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }
}
